package wangm.repository;

import wangm.bean.ExaminationOption;
import wangm.util.DBOperatorComponent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 组合表 T_TOPIC_OPTION_COMBINE  (T_EXAMINATION_OPTION,T_EXAMINATION_TOPIC) repository
 *
 * @author zk
 * @date 2018-02-24 9:41
 */
@Repository
public class ExaminationTopicOptionRepository {
    private static Logger logger = LoggerFactory.getLogger(ExaminationTopicOptionRepository.class);
    /**
     * this table Name
     */
    public static final String thisTableName = "T_TOPIC_OPTION_COMBINE";
    /**
     * db operator
     */
    @Autowired
    private DBOperatorComponent dbOperatorComponent;

    /**
     * 根据小题id获取其下的选项列表（无答案，根据order递增排序）
     *
     * @param tid tid
     * @return List<ExaminationOption></>
     */
    public List<ExaminationOption> getOptionListByTid(String tid){
        String sql = "select o.id as id,o.optionContent as optionContent from T_EXAMINATION_OPTION o " +
                " join T_TOPIC_OPTION_COMBINE toc on(toc.oid = o.id) where toc.tid = ? order by toc.optionOrder";
        try {
            return dbOperatorComponent.getBeanList(ExaminationOption.class,sql,tid);
        } catch (SQLException e) {
            logger.error(e.getMessage(),e);
            return null;
        }
    }
    /**
     * 根据小题的tid获取其正确选项的id
     *
     * @param tid tid
     * @return List<OptionIds></>
     */
    public List<String> getAnswerListByTid(String tid){
        List<String> result = new ArrayList<>();
        String sql = "select oid from T_TOPIC_OPTION_COMBINE where tid = ? and correct = '1'";
        try {
            Object[] objects =  dbOperatorComponent.getArrayList(sql,tid);
            for (int i = 0; i < objects.length; i++) {
                result.add(String.valueOf(objects[i]));
            }
            return result;
        } catch (SQLException e) {
            logger.error(e.getMessage(),e);
            return null;
        }
    }
}
